# -*- coding: UTF-8 -*-
"""
    @Project ：search_cpu_info
    @File ：search_cpu.py
    @Author ：lc
    @Date ：2022.1.19 16:36
"""
import datetime
import json
import re

import requests
import xlrd


def search_cpu(excel_file):
    try:
        # 数据来源网址
        index_url = "https://browser.geekbench.com"
        # 需要查找设备型号
        # s_phone_model = "Meizu 16th"
        devices_list = list()
        for s_phone_model in get_devices_column(excel_file):
            print("当前查询手机：", s_phone_model)
            req = requests.get(f"{index_url}/search?q={s_phone_model}")
            # file = open("t.text", "w", encoding="utf8")
            # file.write(req.text)
            # print("查询到的结果网页：", req.text)
            # 查询匹配到设备型号
            list_model = re.findall(f"<span class='list-col-subtitle'>\nSystem\n</span>\n<a href=(.*?)</a>", req.text)
            # print("匹配到的设备型号和网页：", list_model)
            r_phone_model_list = list()
            page_id_list = list()
            for model in list_model:
                p_id = re.findall('"/v5/cpu/(.*?)">', model)[0]
                page_id_list.append(p_id)
                # 设备名
                r_phone_model_list.append(re.findall(
                    f"<span class='list-col-subtitle'>\nSystem\n</span>\n<a href=\"/v5/cpu/{p_id}\">(.*?)</a>",
                    req.text))
            # print("搜索到设备名：", r_phone_model_list)
            # print("匹配到网页编号列表：", page_id_list)
            # print("列表长度：", len(page_id_list))
            # 设备详情
            list_devices = list()
            phone_dict = dict()
            phone_dict_all = dict()
            # print("开始循环时间：", datetime.datetime.now())
            for num in range(len(page_id_list)):
                dict_device = dict()
                re_info = requests.get(f"{index_url}/v5/cpu/{page_id_list[num]}")
                # file = open("t.text", "w", encoding="utf8")
                # file.write(re_info.text)
                # file_read = open("t.text", "r", encoding="utf8")
                # f = file_read.read()
                # 单核分
                single_core_score = \
                    re.findall("<div class='score'>(.*?)</div>\n<div class='note'>Single-Core Score</div>",
                               re_info.text)[0]
                dict_device["single_core_score"] = single_core_score
                # 多核分
                multi_core_score = \
                    re.findall("<div class='score'>(.*?)</div>\n<div class='note'>Multi-Core Score</div>",
                               re_info.text)[0]
                # print("单核评分：", single_core_score, "多核评分：", multi_core_score)
                dict_device["multi_core_score"] = multi_core_score
                upload_date = re.findall("<td class='system-value timestamp-to-local-full'>(.*?)</td>", re_info.text)[0]
                # print("数据上传时间:", upload_date)
                dict_device["upload_date"] = upload_date
                phone_system = \
                    re.findall("<td class='system-name'>Operating System</td>\n<td class='system-value'>(.*?)</td>",
                               re_info.text)[
                        0]
                dict_device["phone_system"] = phone_system
                # print("手机系统:", phone_system)
                model = \
                    re.findall("<td class='system-name'>Model</td>\n<td class='system-value'>(.*?)</td>", re_info.text)[
                        0]
                if len(re.findall(">(.*?)</a>", model)):
                    dict_device["model"] = re.findall(">(.*?)</a>", model)[0]
                    # print("model：", re.findall(">(.*?)</a>", model)[0])
                else:
                    dict_device["model"] = model
                    # print("model：", model)
                model_id = \
                    re.findall('<tr><td class="name">Model ID</td><td class="value">(.*?)</td></tr>', re_info.text)[0]
                # print("设备型号：", model_id)
                dict_device["model_id"] = model_id
                # 主板
                motherboard = \
                    re.findall("<td class='system-name'>Motherboard</td>\n<td class='system-value'>(.*?)</td>",
                               re_info.text)[0]
                dict_device["motherboard"] = motherboard
                # print("处理器主板：", motherboard)
                cpu_name = \
                    re.findall("<td class='system-name'>Name</td>\n<td class='system-value'>(.*?)</td>", re_info.text)[
                        0]
                if len(re.findall(">(.*?)</a>", cpu_name)):
                    dict_device["cpu_name"] = re.findall(">(.*?)</a>", cpu_name)[0]
                    # print("CPU：", re.findall(">(.*?)</a>", cpu_name)[0])
                else:
                    dict_device["cpu_name"] = cpu_name
                    # print("CPU：", cpu_name)
                # 处理器数量和核心数
                topology = \
                    re.findall("<td class='system-name'>Topology</td>\n<td class='system-value'>(.*?)</td>",
                               re_info.text)[
                        0]
                dict_device["topology"] = topology
                # print("处理器核心数：", topology)
                # 基频
                base_frequency = \
                    re.findall("<td class='system-name'>Base Frequency</td>\n<td class='system-value'>(.*?)</td>",
                               re_info.text)[0]
                # print("处理器基频：", base_frequency)
                dict_device["base_frequency"] = base_frequency
                cluster1 = re.findall('<tr><td class="name">Cluster 1</td><td class="value">(.*?)</td></tr>',
                                      re_info.text)
                if len(cluster1) != 0:
                    dict_device["cluster1"] = cluster1[0]
                    # print("簇1:", cluster1[0])
                cluster2 = re.findall('<tr><td class="name">Cluster 2</td><td class="value">(.*?)</td></tr>',
                                      re_info.text)
                if len(cluster2) != 0:
                    dict_device["cluster2"] = cluster2[0]
                    # print("簇2:", cluster2[0])
                memory = \
                    re.findall("<td class='system-name'>Memory</td>\n<td class='system-value'>(.*?)</td>",
                               re_info.text)[0]
                dict_device["memory"] = memory
                # print("内存:", memory)
                phone_dict[r_phone_model_list[num][0]] = dict_device
                # print("每个手机型号的数据：", phone_dict)
                list_devices.append(phone_dict)
            # print(f"信息：", list_devices)
            phone_dict_all[s_phone_model] = list_devices
            devices_list.append(phone_dict_all)
        j = json.dumps(devices_list)
        j_file = open("devices.json", "w", encoding="utf8")
        j_file.writelines(j)
        return j
    except Exception as e:
        print(e)


def get_devices_column(excel_file):
    book = xlrd.open_workbook(excel_file)
    my_sheet = book.sheet_by_index(0)
    device_list = list()
    for row in my_sheet.get_rows():
        device_list.append(row[3].value)
    return device_list


if __name__ == "__main__":
    # search_cpu()
    print(search_cpu("test.xlsx"))
